package providev.learn.imagina.tema3_2;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class DesktopMemory extends Activity {
	private static final int SHOW_PREFERENCES = 1;
	private static final int MENU_PREFERENCES = Menu.FIRST;
	boolean mostrarImagen = false;
	String nombre = "";
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch (item.getItemId()) {
          case (MENU_PREFERENCES): {
            Intent i = new Intent(this, Preferences.class);
            startActivityForResult(i, SHOW_PREFERENCES);
            return true;
          }
        }
        return false;
      }
    
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, MENU_PREFERENCES, Menu.NONE, R.string.menu_preferences);
        return true;
      }
    
    
    private void updateFromPreferences() {
        Context context = getApplicationContext();
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        mostrarImagen = prefs.getBoolean(Preferences.PREF_MOSTRAR_IMAGEN, false);
        nombre = prefs.getString(Preferences.PREF_NOMBRE, "Imagina");
        TextView txtView = (TextView) findViewById(R.id.textSaludo);
        ImageView imageView = (ImageView) findViewById(R.id.imageView);        
        txtView.setText("Hola " + nombre);
        if(!mostrarImagen)
        {
                        imageView.setVisibility(View.INVISIBLE);
        }
        else
        {
                        imageView.setVisibility(View.VISIBLE);
        }
      }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SHOW_PREFERENCES)
                        updateFromPreferences();
    }


}